package com.onpositive.gamemaker.objects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class BasicGameObjectMap implements IGameObjectMap
{
	
	protected final int width;
	protected final int height;
	protected final int minLevel;
	protected final int maxLevel;
	protected HashMap<Integer, IGameObject[][]> levels; 
	

	public BasicGameObjectMap(int width, int height, int minLevel, int maxLevel) 
	{
		this.width = width;
		this.height = height;
		this.minLevel = minLevel;
		this.maxLevel = maxLevel;
		levels = new HashMap<Integer, IGameObject[][]>();
	}

	@Override
	public int getWidth()
	{
		return width;
	}

	@Override
	public int getHeight()
	{
		return height;
	}

	@Override
	public int getMinLevel()
	{
		return minLevel;
	}

	@Override
	public int getMaxLevel()
	{
		return maxLevel;
	}

	@Override
	public IGameObject[] getGameObjects(int x, int y)
	{
		List<IGameObject> resList = new ArrayList<IGameObject>();
		for (int i = minLevel; i <= maxLevel; i++)
		{
			IGameObject[][] level = levels.get(i);
			if (level != null && level[x][y] != null)
				resList.add(level[x][y]);
		}
		return resList.toArray(new IGameObject[0]);
	}

	@Override
	public IGameObject getGameObject(int x, int y, int level)
	{
		IGameObject[][] levelData = levels.get(level);
		if (levelData == null) //No such layer
			return null;
		return levelData[x][y];
	}
	
	public void putGameObject(int x, int y, int level, IGameObject gameObject)
	{
		IGameObject[][] levelData = levels.get(level);
		if (levelData == null) //No such layer
		{
			levelData = new IGameObject[width][height];
			levels.put(level,levelData);
		}
		levelData[x][y] = gameObject;
		gameObject.setPosition(x,y);
	}

}
